let mongoose = require('mongoose')
let Schema = mongoose.Schema;
const {createHash} = require('../common/')

let teacherSchema = new Schema({
    code : { type : String, match : /^\d{4}$/, required : true, unique: true},
    name : { type : String, match : /^.{1,10}$/, required : true },
    pwd : { type : String, require : true, match : /^[\w-\$]{6,10}$/ },
    identify : { type : Number, enum : [0,1], default : 1 }
})
teacherSchema.pre('save', function(next) {
    // 如果有密码，就先加密再保存
    if(this.pwd) this.pwd = createHash(this.pwd);
    next();
})

module.exports = mongoose.model('teacher', teacherSchema)